<template>
	<view>
		<navbar :title="title" :autoBack="true"></navbar>
		<scroll-view scroll-y="true" class="scbox">
			<view class="con">
				<view class="con__box">
					<!-- 请假 -->
					<view class="con__box__a" v-if="type!=4">
						<view class="" v-if="type==1||type==2">
							<view class="con__box__item">
								<view class="con__box__item__label">
									{{title}}类型
								</view>
								<view class="con__box__item__inp u-flex u-flex-end u-flex-y-center" @click="show=true">
									<view class="con__box__item__inp__timelabel" v-if="!form1.type">
										选择{{title}}类型
									</view>
									<view class="con__box__item__inp__time" v-else>
										{{form1.type}}
									</view>
								</view>
								<image src="/static/homex/more2.png" class="con__box__item__m" mode="aspectFill">
								</image>
							</view>
							<view class="con__box__item" v-if="type==2">
								<view class="con__box__item__label">
									外出地点
								</view>
								<input type="text" class="con__box__item__inp" v-model="form2.addr" placeholder="请输入" />
								<image src="/static/homex/more2.png" class="con__box__item__m" mode="aspectFill">
								</image>
							</view>
						</view>
						<view class="con__box__item">
							<view class="con__box__item__label">
								开始时间
							</view>
							<view class="con__box__item__inp u-flex u-flex-end u-flex-y-center"
								@click="currimetype=1,showtime=true">
								<view class="">
									<view class="con__box__item__inp__timelabel" v-if="!form1.starttime">
										选择日期
									</view>
									<view class="con__box__item__inp__time" v-else>
										{{$u.timeFormat(form1.starttime, 'yyyy-mm-dd')}}
									</view>
								</view>
								<view class=""
									style="width: 2rpx;height: 22rpx;background: #888888;opacity: 0.5;margin: 0 14rpx;">

								</view>
								<view class="">
									<view class="con__box__item__inp__timelabel" v-if="!form1.starttime">
										选择时间
									</view>
									<view class="con__box__item__inp__time" v-else>
										{{$u.timeFormat(form1.starttime, 'hh:MM')}}
									</view>
								</view>
							</view>
							<image src="/static/homex/more2.png" class="con__box__item__m" mode="aspectFill"></image>
						</view>
						<view class="con__box__item">
							<view class="con__box__item__label">
								结束时间
							</view>
							<view class="con__box__item__inp u-flex u-flex-end u-flex-y-center"
								@click="currimetype=2,showtime=true">
								<view class="">
									<view class="con__box__item__inp__timelabel" v-if="!form1.endtime">
										选择日期
									</view>
									<view class="con__box__item__inp__time" v-else>
										{{$u.timeFormat(form1.endtime, 'yyyy-mm-dd')}}
									</view>
								</view>
								<view class=""
									style="width: 2rpx;height: 22rpx;background: #888888;opacity: 0.5;margin: 0 14rpx;">

								</view>
								<view class="">
									<view class="con__box__item__inp__timelabel" v-if="!form1.endtime">
										选择时间
									</view>
									<view class="con__box__item__inp__time" v-else>
										{{$u.timeFormat(form1.endtime, 'hh:MM')}}
									</view>
								</view>
							</view>
							<image src="/static/homex/more2.png" class="con__box__item__m" mode="aspectFill"></image>
						</view>
						<view class="con__box__item">
							<view class="con__box__item__label">
								{{title}}时长
							</view>
							<input type="number" class="con__box__item__inp" v-model="form1.often" placeholder="请输入" />
							<view class="con__box__item__qhbox__i" v-if="type==3" style="margin-left: 10rpx;">
								小时
							</view>
							<view class="con__box__item__qhbox" v-if="type==1||type==2" style="margin-left: 40rpx;">
								<view class="con__box__item__qhbox__i" v-for="(item,index) in timelist" :key="index"
									:class="curt==index?'ac':''" @click="curt=index,form1.unit=item">
									{{item}}
								</view>
							</view>
						</view>
						<view class="" v-if="type==2">
							<view class="con__box__item">
								<view class="con__box__item__label">
									随行同事
								</view>
								<input type="text" class="con__box__item__inp" v-model="form2.colleagues"
									placeholder="请输入" />
								<image src="/static/homex/more2.png" class="con__box__item__m" mode="aspectFill">
								</image>
							</view>
							<view class="con__box__item">
								<view class="con__box__item__label">
									拜访客户
								</view>
								<input type="text" class="con__box__item__inp" v-model="form2.customer"
									placeholder="请输入" />
								<image src="/static/homex/more2.png" class="con__box__item__m" mode="aspectFill">
								</image>
							</view>
						</view>
					</view>
					<view class="con__box__a" v-else>
						<view class="con__box__item">
							<view class="con__box__item__label">
								审批类型
							</view>
							<input type="text" class="con__box__item__inp" v-model="form3.type" placeholder="请输入" />
							<image src="/static/homex/more2.png" class="con__box__item__m" mode="aspectFill"></image>
						</view>
					</view>
					<u-gap height="22rpx" bgColor="#F8F8F8"></u-gap>
					<view class="con__box__b">
						<view class="con__box__title">
							<view class="con__box__title__line">

							</view>
							{{title}}内容
						</view>
						<u--textarea :showConfirmBar='false' v-model="sameform.content" autoHeight placeholder="请输入..."
							border='none' :placeholderStyle='{color:"#919191",fontSzie:"24rpx",}'></u--textarea>
						<!-- <image src="/static/tool/up1.png" class="con__box__up" mode=""></image> -->
						<view class="" style="margin: 35rpx 0 40rpx 0;">
							<u-upload :fileList="fileList6" @afterRead="afterRead" @delete="deletePic" name="6" multiple
								:maxCount="9" width="149rpx" height="149rpx">
								<image src="/static/tool/up1.png" mode="widthFix" style="width: 149rpx;height: 149rpx;">
								</image>
							</u-upload>
						</view>
						<view class="con__box__title u-flex-between">
							<view class="u-flex u-flex-y-center">
								<view class="con__box__title__line">

								</view>
								审批人
							</view>
							<image src="/static/tool/question.png" class="con__box__title__wh" mode=""></image>
						</view>
						<image :src="manager?URL(manager.manager_avatar):'/static/tool/spr.png'"
							@click="navto('/pages/customer/checkxs')" class="con__box__spr" mode="aspectFill"></image>
						<u-button color="#3262FD" @click="submit" text="提交" :loading="loading"
							:customStyle='{width:"628rpx",height:"94rpx",margin:0,padding:0,borderRadius:"15rpx",fontSize:"32rpx"}'></u-button>
					</view>
				</view>
			</view>
		</scroll-view>
		<!-- 选择类型弹框 -->
		<u-picker :show="show" :columns="type==1?columns:columns2" closeOnClickOverlay @close='show=false'
			@cancel='show=false' @confirm='confirm' :defaultIndex='defaultIndex'></u-picker>
		<!-- 时间 -->
		<u-datetime-picker ref="datetimePicker" :show="showtime" weeks='datetime' @close='showtime=false'
			@cancel='showtime=false' @confirm='confirmtime' closeOnClickOverlay v-model="value1" mode="datetime"
			:formatter="formatter"></u-datetime-picker>
	</view>
</template>

<script>
	import sale from '@/api/_sale.js'

	export default {
		data() {
			return {
				fileList6: [],
				show: false,
				showtime: false,
				value1: Number(new Date()),
				currimetype: 1,
				columns: [
					[
						'年假', '事假', '病假', '调休', '婚假', '产假', '陪产假', '其他',
					]
				],
				columns2: [
					[
						'外出', '出差', '培训', '招待', '拜访客户', '展览会议', '其他',
					]
				],
				timelist: ['天', '小时'],
				curt: 0,
				value2: '',
				title: '',
				type: '',
				sameform: { //相同部分表单
					content: '',
					images: ''
				},
				form1: { //请假表单
					type: '',
					starttime: '',
					endtime: '',
					often: '',
					unit: '天',
				},
				form2: { //外出表单
					addr: '',
					colleagues: '',
					customer: ''
				},
				form3: {
					type: ''
				},
				manager_id: '',
				defaultIndex: [0],
				manager: null,
				loading:false
			};
		},
		onLoad(o) {
			this.title = o.title
			this.type = o.type
		},
		methods: {
			async submit() {
				let _this = this
				let arr = []
				let obj = {}
				this.fileList6.map(t => {
					arr.push(t.url.url)
				})
				this.sameform.images = arr.join(',')
				let {
					type,
					starttime,
					endtime,
					often
				} = this.form1
				let {
					addr
				} = this.form2
				if (this.type == 1) { //请假
					if (!type || !starttime || !endtime || !often) {
						uni.$u.toast('请假类型,开始时间,结束时间,请假时长为必填项')
						return
					}
					obj = Object.assign({}, _this.form1, _this.sameform)
				}
				if (this.type == 2) { //外出
					if (!type || !starttime || !endtime || !often || !addr) {
						uni.$u.toast('外出类型,开始时间,结束时间,外出时长,外出地点为必填项')
						return
					}
					obj = Object.assign({}, _this.form1, _this.form2, _this.sameform)
				}
				if (this.type == 3) { //加班
					this.form1.unit = '小时'
					if(!starttime||!endtime||!often){
						uni.$u.toast('开始时间,结束时间,加班时长为必填项')
						return
					}
					obj = Object.assign({}, _this.form1, _this.sameform)
				}
				if (this.type == 4) { //其他
					if(!this.form3.type){
						uni.$u.toast('审批类型为必填项')
						return
					}
					obj = Object.assign({}, _this.form3, _this.sameform)
				}
				if(!this.manager){
					uni.$u.toast('请选择审批人')
					return
				}
				// console.log(obj)
				let form_json=JSON.stringify(obj)
				// console.log(form_json)
				this.manager_id=this.manager.manager_id
				this.loading=true
				let res=await sale.createExam({type:this.type,form_json,manager_id:this.manager_id})
				if(res.data.code==1){
					uni.$u.toast('提交成功')
					setTimeout(()=>{
						_this.loading=false
						_this.$store.commit('setuser',null)
						uni.navigateBack()
					},1000)
					
				}else{
					this.loading=false
				}
				
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				let _this = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: "http://dn.fuliaoxx.com/api/common/upload", // 仅为示例，非真实的接口地址
						filePath: url,
						name: "file",
						formData: {
							user: "test",
						},
						success: (res) => {
							setTimeout(() => {
								resolve(JSON.parse(res.data).data);
							}, 1000);
						},
					});
				});
			},

			confirm(e) {
				this.form1.type = e.value[0]
				this.defaultIndex = e.indexs
				this.show = false
			},
			confirmtime(e) {
				if (this.currimetype == 1) {
					this.form1.starttime = e.value
				} else {
					this.form1.endtime = e.value
				}
				this.showtime = false
			},
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`
				}
				if (type === 'month') {
					return `${value}月`
				}
				if (type === 'day') {
					return `${value}日`
				}

				return value
			},
		},
		onShow() {
			console.log(this.$store.state.curuser)
			if (this.$store.state.curuser) {
				this.manager = this.$store.state.curuser
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f8f8f8;
	}

	.con {
		padding: 20rpx 30rpx;

		&__box {
			background: #FFFFFF;
			border-radius: 20rpx;

			&__a {
				padding: 8rpx 0 0 32rpx;
			}

			&__b {
				padding: 33rpx 30rpx 44rpx;
			}

			&__item {
				height: 104rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding-right: 32rpx;
				border-bottom: 2rpx solid #F9F9F9;

				&__label {
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 26rpx;
					color: #6D6D6D;
					line-height: 26rpx;
				}

				&__m {
					width: 12rpx;
					height: 21rpx;
					margin-left: 10rpx;
				}

				&__inp {
					flex: 1;
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 26rpx;
					color: #020202;
					line-height: 38rpx;
					text-align: right;

					.uni-input-placeholder {
						font-family: PingFang SC;
						font-weight: 500;
						font-size: 26rpx;
						color: #6D6D6D;
						line-height: 38rpx;
					}

					&__timelabel {
						font-family: PingFang SC;
						font-weight: 500;
						font-size: 26rpx;
						color: #6D6D6D;
						line-height: 38rpx;
					}
				}

				&__qhbox {
					width: 146rpx;
					height: 60rpx;
					background: #F8F8F8;
					border-radius: 8rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					margin-left: 12rpx;

					&__i {
						width: 70rpx;
						height: 55rpx;
						background: #F8F8F8;
						border-radius: 8rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						font-family: PingFang SC;
						font-weight: 500;
						font-size: 24rpx;
						color: #010101;
						line-height: 24rpx;
					}

					.ac {
						background: #3262FD;
						color: #FFFFFF;
					}
				}
			}

			&__title {
				display: flex;
				align-items: center;
				font-family: PingFang SC;
				font-weight: 500;
				font-size: 26rpx;
				color: #1E1E1E;
				line-height: 26rpx;
				margin-left: 7rpx;

				&__line {
					width: 6rpx;
					height: 24rpx;
					background: #3262FD;
					margin-right: 10rpx;
				}

				&__wh {
					width: 30rpx;
					height: 29rpx;
				}
			}

			&__up {
				width: 149rpx;
				height: 149rpx;
				margin-bottom: 40rpx;
				margin-top: 35rpx;
			}

			&__spr {
				width: 113rpx;
				height: 113rpx;
				border-radius: 50%;
				margin: 44rpx 0 58rpx;
			}
		}
	}

	::v-deep.u-textarea {
		padding: 30rpx 4rpx;

		.u-textarea__field {
			font-size: 26rpx;
			color: #020202;
		}

		.uni-textarea-placeholder {
			color: #939393 !important;
		}
	}
</style>